﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.DAL.DocumentCenter;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using Office = Microsoft.Office.DocumentManagement.DocumentSets;
using MS.InformationManagement.DAL.RecordCenter;

namespace MS.InformationManagement.Core.IncommingDocuments
{
    public class AddIncommingDocumentAction : IBaseCommand
    {

        #region fields

        Office.DocumentSet _currentRadicadoEntity;

        #endregion

        #region Constructors

        public AddIncommingDocumentAction(Office.DocumentSet currentRadicado)
        {
            _currentRadicadoEntity = currentRadicado;
        }

        #endregion

        #region Implemented Methods

        public void Execute()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb currentWeb = currentSite.OpenWeb())
                    {
                        var selectedSeccion = RecordSiteQuery.QueryRecordsSiteHelper<Seccion>.QueryRecordsEntityById(GlobalConstants.LIST_BIBLIOTECA_SECCION, int.Parse(_currentRadicadoEntity.Folder.Item[CustomSiteColumns.FIELD_DOCSET_DEPARTMNET].ToString()));
                        SPListItem newItem = currentWeb.Lists[GlobalConstants.LIST_RADICADOS_ENTRANTES].Items.Add();
                        newItem[CustomSiteColumns.FIELD_INCOMMING_OWNER] = selectedSeccion.UsuarioEncargadoCorrespondencia;
                        newItem[CustomSiteColumns.FIELD_INCOMMING_RADICADO] = _currentRadicadoEntity.Folder.Item[CustomSiteColumns.FIELD_DOCSET_ID_RADICADO];
                        newItem[CustomSiteColumns.FIELD_INCOMMING_DEPARTMENT] = selectedSeccion.Title;
                        newItem[CustomSiteColumns.FIELD_INCOMMING_DOCUMENTAL_TYPE] = _currentRadicadoEntity.Folder.Item[CustomSiteColumns.FIELD_DOCUMENT_DOCUMENTAL_TYPE];
                        newItem[CustomSiteColumns.FIELD_INCOMMING_SENDER] = _currentRadicadoEntity.Folder.Item[CustomSiteColumns.FIELD_DOCSET_SENDERRECEPT];
                        newItem[SPBuiltInFieldId.Title] = _currentRadicadoEntity.Folder.Item[CustomSiteColumns.FIELD_DOCSET_ID_RADICADO];
                        newItem.Update();
                    }
                }
            });
        }

        public void Undo()
        {
            //TODO: Eliminar item creado
        }

        #endregion


    }
}
